/**
 *  基本Activity类：
 *  1.初始化标题栏
 *
 */
package com.hzf.booklibrary.base;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.hzf.booklibrary.R;
import com.hzf.booklibrary.ReaderApplication;
import com.hzf.booklibrary.component.AppComponent;
import com.hzf.booklibrary.okhttp.ResultCallBack;
import com.hzf.booklibrary.ui.fragment.RecommendFragment;
import com.hzf.booklibrary.utils.DeviceUtils;
import com.hzf.booklibrary.utils.LogUtils;
import com.hzf.booklibrary.utils.NetworkUtils;
import com.hzf.booklibrary.utils.SharedPreferencesUtil;
import com.hzf.booklibrary.utils.StatusBarCompat;
import com.hzf.booklibrary.view.loadding.CustomDialog;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import okhttp3.Request;

public abstract class BaseActivity extends AppCompatActivity {


    protected Context mContext;
    protected int statusBarColor = 0;
    protected View statusBarView = null;
    private boolean mNowMode;
    private CustomDialog dialog;//进度条
    protected Button mBtnRight;
    protected Button mBtnLeft;
    protected TextView tvCenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(getLayoutId());
        if (statusBarColor == 0) {
            statusBarView = StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.colorPrimaryDark));
        } else if (statusBarColor != -1) {
            statusBarView = StatusBarCompat.compat(this, statusBarColor);
        }
        //兼容版本
        transparent19and20();
        mContext = this;

        ButterKnife.bind(this);
        mBtnLeft = ButterKnife.findById(this,R.id.btn_left);

        mBtnRight = ButterKnife.findById(this,R.id.btn_right);
        tvCenter = ButterKnife.findById(this,R.id.tv_title);


        if(Constant.API_BASE_URL.equals( "")){
            NetworkUtils.getBookServerIp(this, new ResultCallBack() {
                @Override
                public void onError(Request request, Exception e) {

                }

                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject json = new JSONObject(response);
                        String ip = json.getString("school_clture_service_url");
                        Constant.API_BASE_URL = ip+"/";

                        setupActivityComponent(ReaderApplication.getsInstance().getAppComponent());
                        initDatas();
                        //初始化标题栏
                        initTitleBar();
                        configViews();
                        mNowMode = SharedPreferencesUtil.getInstance().getBoolean(Constant.ISNIGHT);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }else{
            setupActivityComponent(ReaderApplication.getsInstance().getAppComponent());
            initDatas();
            //初始化标题栏
            initTitleBar();
            configViews();
            mNowMode = SharedPreferencesUtil.getInstance().getBoolean(Constant.ISNIGHT);
        }


    }

    protected void transparent19and20() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        dismissDialog();
    }

    public abstract int getLayoutId();

    protected abstract void setupActivityComponent(AppComponent appComponent);

    public abstract void initTitleBar();

    public abstract void initDatas();

    /**
     * 对各种控件进行设置、适配、填充数据
     */
    public abstract void configViews();

    protected void gone(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }

    protected void visible(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.VISIBLE);
                }
            }
        }

    }

    protected boolean isVisible(View view) {
        return view.getVisibility() == View.VISIBLE;
    }

    // dialog--加载动画框
    public CustomDialog getDialog() {
        if (dialog == null) {
            dialog = CustomDialog.instance(this);
            dialog.setCanceledOnTouchOutside(true);
        }
        return dialog;
    }

    public void hideDialog() {
        if (dialog != null)
            dialog.hide();
    }

    public void showDialog() {
        getDialog().show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    //隐藏状态栏
    protected void hideStatusBar() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
        if(statusBarView != null){
            statusBarView.setBackgroundColor(Color.TRANSPARENT);
        }
    }
    //显示状态栏
    protected void showStatusBar() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
        if(statusBarView != null){
            statusBarView.setBackgroundColor(statusBarColor);
        }
    }

}
